﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using TRicheControl.WinformControl;
//using System.Reflection.Context;

namespace TRicheControl.Editor.Forms
{
    public partial class frmRicheMenuBarItemEditor : Form
    {
        private Dictionary<string, RicheMenuBarItem> mRicheMenuBarItems = new Dictionary<string, RicheMenuBarItem>();
        private RicheMenuBarItemCollection mRicheMenuBarItemCollection = new RicheMenuBarItemCollection();
        public RicheMenuBarItemCollection RicheMenuBarItems
        {
            get
            {
                return mRicheMenuBarItemCollection;
            }
        }

        private Dictionary<string, Type> mMenuItemTypes = new Dictionary<string,Type>();

        private int mCount = 1;

        public frmRicheMenuBarItemEditor(object value)
        {
            InitializeComponent();
            InitMenuItemTypes();
            InitMenuItems(value as RicheMenuBarItemCollection);
            this.propertyGrid.BrowsableAttributes = new AttributeCollection(new BrowsableAttribute(false), new ReadOnlyAttribute(false));
        }

        private void InitMenuItemTypes()
        {
            Assembly assembly = Assembly.GetAssembly(typeof(RicheMenuBarItem));
            foreach (Type itemType in assembly.GetTypes())
            {
                if (itemType.BaseType == typeof(RicheMenuBarItem))
                {
                    this.comboBoxTypes.Items.Add(itemType.Name);
                    mMenuItemTypes.Add(itemType.Name, itemType);
                }
            }
            if (this.comboBoxTypes.Items.Count > 0)
            {
                this.comboBoxTypes.SelectedItem = this.comboBoxTypes.Items[0];
            }
        }

        private void InitMenuItems(RicheMenuBarItemCollection items)
        {
            if (items == null)
                return;
            foreach (RicheMenuBarItem item in items)
            {
                if (mRicheMenuBarItemCollection.Contains(item))
                    continue;
                mRicheMenuBarItems.Add(item.Name, item);
                mRicheMenuBarItemCollection.Add(item);
                this.listBoxItemList.Items.Add(item.Name);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (this.comboBoxTypes.SelectedItem == null)
                return;
            Type menuType = mMenuItemTypes[Convert.ToString(this.comboBoxTypes.SelectedItem)];
            RicheMenuBarItem obj = menuType.Assembly.CreateInstance(menuType.FullName) as RicheMenuBarItem;
            obj.Name = menuType.Name + mCount;
            mRicheMenuBarItems.Add(obj.Name, obj);
            mRicheMenuBarItemCollection.Add(obj);
            this.listBoxItemList.Items.Add(obj.Name);
            mCount++;
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            string item = Convert.ToString(this.listBoxItemList.SelectedItem);
            if(string.IsNullOrEmpty(item))
                return;
            this.listBoxItemList.Items.Remove(item);
            RicheMenuBarItem baritem = this.mRicheMenuBarItems[item];
            this.mRicheMenuBarItems.Remove(item);
            mRicheMenuBarItemCollection.Remove(baritem);
            if (this.listBoxItemList.Items.Count > 0)
            {
                this.listBoxItemList.SelectedItem = this.listBoxItemList.Items[this.listBoxItemList.Items.Count - 1];
            }
        }

        private void listBoxItemList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string objName = Convert.ToString(this.listBoxItemList.SelectedItem);
            if (!mRicheMenuBarItems.ContainsKey(objName))
                return;
            RicheMenuBarItem item = mRicheMenuBarItems[objName];
            this.propertyGrid.SelectedObject = item;
        }
    }
}
